/**
* Copyright (c) 2015 UNI-TREND Co., Ltd.
* All rights reserved.  
*  
* @file        EffiCal.h
* @brief      cal efficiency of any process
* @version     1.0 
* @author      
* @Email       
* @date        2015-11-19
*/
#include "EffiCal.h"
#include "Log.h"
#include <iostream>
using namespace std;

CEffiCal::EFFI_RECORD_TYPE CEffiCal::s_m_effi_record;

CEffiCal::CEffiCal(char* pFlag)
{
    // get start time/num
    m_record = GetTime();
    m_flag = string(pFlag);
}

CEffiCal::CEffiCal(string flag)
{
    // get start time/num
    m_record = GetTime();
    m_flag = flag;
}

CEffiCal::~CEffiCal()
{
    // get end time/num
    m_record = GetTime() - m_record;
    CEffiCal::s_m_effi_record.insert(CEffiCal::EFFI_RECORD_INSERT_TYPE(m_flag,m_record));
    // debug-code // output to console
    cout<<"[EFFI]"<<m_flag<<"="<<m_record<<endl;
}

string CEffiCal::GetFlagByIndex(int index)
{
    int cnt = 0;
    CEffiCal::EFFI_RECORD_IT_TYPE it = CEffiCal::s_m_effi_record.begin();  
    while((it != CEffiCal::s_m_effi_record.end()))
    {
        it++;
        if(cnt++ == index)
        {
            return it->first;
        }
    }

    // fail to find
    return NULL;
}
COST_TYPE CEffiCal::GetCostByIndex(int index)
{
    int cnt = 0;
    CEffiCal::EFFI_RECORD_IT_TYPE it = CEffiCal::s_m_effi_record.begin();  
    while((it != CEffiCal::s_m_effi_record.end()))
    {
        it++;
        if(cnt++ == index)
        {
            return it->second;
        }
    }

    // fail to find
    return 0;
}

void CEffiCal::Output2Log()
{
    char log[50];
    CEffiCal::EFFI_RECORD_IT_TYPE it = CEffiCal::s_m_effi_record.begin();  
    while((it != CEffiCal::s_m_effi_record.end()))
    {
        memset(log,0,50);
        sprintf(log,"[%s=%d]",it->first.c_str(), it->second);
        CLog::info(log,CLog::LOG_2END);
        
        it++;
    }
}

